﻿Imports System.Runtime.InteropServices
Public Class frmCheckData
    Dim SelectCheck As CheckMode
    Dim CanClearText As Boolean = False
    '设置键盘焦点
    Private Declare Function SetFocus Lib "user32" _
                    Alias "SetFocus" (ByVal hWnd As IntPtr) As Integer

    Private Sub CheckData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cbo_CheckMode.Items.Clear()
        If CheckData IsNot Nothing Then
            For i = 0 To CheckData.Count - 1
                Cbo_CheckMode.Items.Add(CheckData.Item(i).CheckName)
            Next
        End If
        If Cbo_CheckMode.Items.Count > 0 Then Cbo_CheckMode.SelectedIndex = 0
        ChangeButtonColor(Btn_OK)
    End Sub

    Private Sub LB_CheckDescription_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles LB_CheckDescription.KeyPress
        e.Handled = True
    End Sub

    Private Sub Cbo_CheckMode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo_CheckMode.SelectedIndexChanged
        SelectCheck = getCheckFunName(Cbo_CheckMode.Items.Item(Cbo_CheckMode.SelectedIndex))
        LB_CheckDescription.Text = SelectCheck.CheckDescription
    End Sub
    Private Function getCheckFunName(ByVal CheckName As String) As CheckMode
        Dim result As CheckMode = Nothing
        If CheckData IsNot Nothing Then
            If CheckData.Count > 0 Then
                For i = 0 To CheckData.Count - 1
                    If UCase(CheckData.Item(i).CheckName) = UCase(CheckName) Then result = CheckData.Item(i) : Exit For
                Next
                Return result
            Else
                Return Nothing
            End If
        Else
            Return Nothing
        End If
    End Function

    Private Sub Btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OK.Click
        Try
            Dim CheckString As String = TB_InputData.Text
            Dim tmp As String
            Dim a As Integer = SelectCheck.CheckLength
            Dim OldCheck As String = "", NewCheck As String
            If String.IsNullOrEmpty(CheckString) Then Return
            If a <= 0 Or SelectCheck.CheckName.ToUpper = "nocheck".ToUpper Then MsgBox("请选择校验方式！", MsgBoxStyle.Exclamation, "提示") : Return
            tmp = CheckString.Substring(0, CheckString.Length - a)
            OldCheck = CheckString.Substring(CheckString.Length - a, a)
            NewCheck = Form1.getCheckNumber(SelectCheck, tmp)
            If OldCheck = NewCheck Then
                MsgBox("校验码计算正确！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示")
            Else
                MsgBox("错误的校验码，正确的校验码应为：" & NewCheck, MsgBoxStyle.Critical, "校验失败")
            End If
        Catch ex As Exception
            MsgBox("校验码计算失败：" & ex.Message, MsgBoxStyle.Critical, "错误")
        End Try
        SetFocus(TB_InputData.Handle.ToInt32)
    End Sub

    Private Sub TB_InputData_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_InputData.KeyPress
        If CanClearText = True Then
            TB_InputData.Text = ""
            CanClearText = False
        End If
        If Asc(e.KeyChar) = 13 Then
            Btn_OK_Click(sender, e)
            CanClearText = True
        End If
    End Sub
End Class